package tools;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.LineNumberReader;
import java.io.StringWriter;

/*
 * CAUTION: THIS MODIFIES ALL SOURCE!!  IT ADDS THE LICENSE HEADER
 */
public class AddLicenseHeader 
extends org.apache.tools.ant.Task {
  
  // ----------------------------------------- Class Variables
  
  protected static final String NL =
    System.getProperty("line.separator");
  
  // -------------------------------------- Instance Variables
  
  File javaSrc = null;
  String hdrTxtFile = null;
  String hdrString;
  boolean isJava;
  boolean isProject;
  org.apache.tools.ant.types.FileSet srcSet = null;
  
  // -------------------------------------------- Constructors
  
  /**
   * Add the license-header
   *
   * @param file the java source file to process
   * @param hdr the license-header file
   *
   * @throws Exception if any error occurs
   */
  public AddLicenseHeader(String file, String hdr)
  throws Exception {
    
    isJava = file.endsWith(".java");
    isProject = file.equals("project.xml");
    javaSrc = new File(file);
    setHdrString(new File(hdr));
    if ( !javaSrc.canRead() ) {
      throw new Exception
      ("Cannot read source file: " + file); 
    }
    
  }
  
  /**
   * No arg constructor for the Ant Task
   *
   * @throws Exception if any error occurs
   */
  public AddLicenseHeader() {
    
  }
  
  // ------------------------------------------ Public Methods
  
  public static void main (String[] args) throws Exception {
    
    if (args.length != 2) {
      System.out.println
      ("Usage: java " + AddLicenseHeader.class + 
      " [file] [hdr]");
      return;
    }
    AddLicenseHeader licenseAdder = 
      new AddLicenseHeader(args[0], args[1]);
    try {
      licenseAdder.addLicense();
    } catch (Exception e) {
      e.printStackTrace();
    }
    
  }
  
  public void execute() 
  throws org.apache.tools.ant.BuildException {
    
    try {
      org.apache.tools.ant.Project p = 
        srcSet.getProject();
      org.apache.tools.ant.DirectoryScanner ds = 
        srcSet.getDirectoryScanner(p);
      File baseDir = ds.getBasedir();
      setHdrString(new File(baseDir, getHdrTxtFile()));
      String[] sourceFiles = ds.getIncludedFiles();
      for (int i = 0; i < sourceFiles.length; i++) {
        setJavaSrc(new File(baseDir, sourceFiles[i]));
        addLicense();
      }
    } catch (Exception e) {
      throw new org.apache.tools.ant.BuildException(e); }
    
  }
  
  public void addLicense() throws Exception {
    
    if (!javaSrc.canWrite()) {
      throw new Exception("cannot write to file.");
    }
    String fileName = javaSrc.getName();
    isJava = fileName.endsWith(".java");
    isProject = fileName.equals("project.xml");
    if ( !isJava && !isProject ) return;
    LineNumberReader srcLineReader = 
      new LineNumberReader(new FileReader(javaSrc));
    File temp = new File("addlicense.tmp");
    FileWriter writer = new FileWriter(temp);
    if ( isProject ) {
      writer.write
      ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + NL);
      writer.write("<!--" + NL);
      writer.write(getHdrString()+NL);
      writer.write("-->" + NL);
      String line = srcLineReader.readLine();
      if ( line == null ) return;
      while ( line != null && !line.endsWith("?>")) {
        line = srcLineReader.readLine();
      }
    }
    if ( isJava ) {
      writer.write(getHdrString()+NL);
    }
    int c = srcLineReader.read();
    while ( c > 0 ) {
      writer.write(c);
      c = srcLineReader.read();
    }
    writer.flush();
    writer.close();
    javaSrc.delete();
    javaSrc.createNewFile();
    writer = new FileWriter(javaSrc);
    FileReader reader = new FileReader(temp);
    c = reader.read();
    while ( c > 0 ) {
      writer.write(c);
      c = reader.read();
    }
    writer.flush();
    writer.close();
    
  }
  
  public void setJavaSrc(File f) { javaSrc = f; }
  public void addConfiguredSrcSet
  (org.apache.tools.ant.types.FileSet fs) { 
    srcSet = fs; }
  
  public String getHdrTxtFile() {
    return hdrTxtFile;
  }
  
  public void setHdrTxtFile(String hdrTxtFile) {
    this.hdrTxtFile = hdrTxtFile;
  }
  
  public String getHdrString() {
    return hdrString;
  }
  
  public void setHdrString(File file) throws Exception {
    
    if ( !file.canRead() ) {
      throw new Exception("Cannot read license header: " + file);
    }
    FileReader licReader = new FileReader(file);
    StringWriter strWriter = new StringWriter();
    int c = licReader.read();
    if ( c < 0 ) throw new Exception("empty bad license file");
    while ( c > 0 ) {
      strWriter.write(c);
      c = licReader.read();
    }
    strWriter.flush();
    strWriter.close();
    this.hdrString = strWriter.toString();
    
  }
  
}
